package aop.aspectj;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

@Aspect
// @Order(0)  //设置优先级，值越低优先级越高
public class MyAspectj {
    //切点
    @Pointcut("execution(* aop.bean.Cat.getName())")
    private void getName(){}
    @Pointcut("execution(* aop.bean.Cat.printThrowException())")
    private void th(){}

    //advice
    @Before("getName()")
    public void before(){
        System.out.println("MyAspectj.before");
    }
    @After("execution(* aop.bean.Cat.getName())") //内置切点形式
    public void after(){
        System.out.println("MyAspectj.after");
    }
    @AfterReturning(pointcut = "getName()",returning = "value")
    public void afterReturning(Object value){
        System.out.println("MyAspectj.afterReturning:"+value);
    }
    @AfterThrowing(pointcut = "th()",throwing = "exception")
    public void afterThrowing(Exception exception){
        System.out.println("MyAspectj.afterThrowing");
        System.out.println("exception = " + exception);
    }

    @Around("execution(* aop.bean.Cat.getAge())")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        //前置通知
        System.out.println(pjp.getSignature().getName()+"方法执行前");
        Object result = pjp.proceed();
        System.out.println(pjp.getSignature().getName()+"方法执行后");
        return result;
    }

}
